************************************************************************************************************
** Replication file for "Image of China among the Taiwanese Public: Results from Two List Experiments"
** Chung-li Wu and Alex Min-Wei Lin                                                       
** All commands run using Stata 15
** Need to install the user-defined "kict" and "coefplot" packages       
************************************************************************************************************

// set working directory (from where you will upload/save data) //
*cd "your path/directory here" 

// install coefplot and kict packages //
ssc install coefplot, replace
net install st0559.pkg, from("http://www.stata-journal.com/software/sj19-2/") replace

// open file named "2021 September China Image Survey.dta" //
use "2021 September China Image Survey.dta", clear

set scheme cleanplots, perm
set more off, permanently
rename wr_1 wf


// VARIABLES IN DATA //

* Recode tc1 (gender)
recode tc1 (2=1) (1=0), gen (female)
label variable female "Gender (female=1)"
label define female 0 "Male" 1 "Female"
label values female female

* Recode v1 (age)
mvdecode v1, mv (97 98)
gen age=110-v1
label variable age "Age"
recode age (20/39=1)(40/59=2)(60/max=3), gen (age3)
label variable age3 "Age (by groups)"
label define agegroups3 1 "20-39 yrs" 2 "40-59 yrs" 3 "60 and over"
label values age3 agegroups3

* Recode v51 (education)
recode v51 (1 2 3 4=1)(5=2)(6 7 8=3)(9 98=.), gen (education2)
label variable education2 "Education"
label define edu2 1 "Junior high or below" 2 "High school" 3 "Some college and above"
label values education2 edu2

* Recode v50 (ethnicity)
recode v50 (1=1)(2=2)(3=3)(4 5 97 98=.), gen (ethnicity)
label variable ethnicity "Ethnicity"
label define ethnicity 1 "Hakka" 2 "Minnan" 3 "Mainlander"
label values ethnicity ethnicity

* Recode v49 (partyid)
recode v49 (1 4 5 6 11=1)(2 3 7 8 9 10 12 =2)(13 14 15 94 95 97=3)(98=.), gen (partyid)
label variable partyid "Party identification"
label define partyid 1 "Pan-Blue" 2 "Pan-Green" 3 "Independent"
label values partyid partyid

* Recode v48 (natl_id)
recode v48 (1=1)(2=3)(3=2)(4 94 95 97 98=.), gen (natl_id)
label variable natl_id "National identity"
label define natl_id 1 "Taiwanese" 2 "Chinese" 3 "Both"
label values natl_id natl_id

* Recode v21 (image of china)
recode v21 (1/2=3)(3/4=1)(5/97=2)(98=.), gen (china_image2)
label variable china_image2 "Overall impression of China"
label define image2 1 "Poor/Very poor" 2 "Average/DK" 3 "Good/Very good"
label values china_image2 image2


// List Experiment //

* Recode v35 (Friend (control condition))
recode v35 (1=3) (2=2) (3=1) (4=0) (5/98=.), gen (friend_base)
label variable friend_base "Friend (control)"
* Recode v36 (Enemy (treatment condition))
recode v36 (1=4) (2=3) (3=2) (4=1) (5=0) (6/98=.), gen (enemy_test)
label variable enemy_test "Enemy (treatment)"
* Recode v37 (Enemy (control condition))
recode v37 (1=3) (2=2) (3=1) (4=0) (94/98=.), gen (enemy_base)
label variable enemy_base "Enemy (control)"
* Recode v38 (Friend (treatment condition)
recode v38 (1=4) (2=3) (3=2) (4=1) (5=0) (6/97=.), gen (friend_test)
label variable friend_test "Friend (treatment)"

* Creating list experiment variables
gen t=.
replace t=0 if friend_test<5
replace t=1 if enemy_test<5
label variable t "Experiment condition: test"
label define t 0 "friend treatment" 1 "enemy treatment"
label values t t

gen b=.
replace b=0 if friend_base<4
replace b=1 if enemy_base<4
label variable b "Experiment condition: control"
label define b 0 "friend control" 1 "enemy control"
label values b b

gen s=.
replace s=0 if ((b==0) & (t==1))
replace s=1 if ((b==1) & (t==0))
label variable s "Type of sample"
label define s 0 "Half-sample A" 1 "Half-sample B"
label values s s

gen cat_friend=.
replace cat_friend=0 if friend_base<4
replace cat_friend=1 if friend_test<5
label variable cat_friend "Experiment category: friend"
label define cat_friend 0 "friend_control" 1 "friend_treatment"
label values cat_friend cat_friend

gen cat_enemy=.
replace cat_enemy=0 if enemy_base<4
replace cat_enemy=1 if enemy_test<5
label variable cat_enemy "Experiment category: enemy"
label define cat_enemy 0 "enemy_control" 1 "enemy_treatment"
label values cat_enemy cat_enemy

gen friend_count=friend_base
replace friend_count=friend_test if mi(friend_base)
label variable friend_count "Item count variable for China as a friend"
gen enemy_count=enemy_base
replace enemy_count=enemy_test if mi(enemy_base)
label variable enemy_count "Item count variable for China as an enemy"

* Direct question (DQ)
* Recode v39 (direct question)
recode v39 (1=1) (2=2) (3=3) (4=4) (5/97=5) (98=.), gen (dq1)
label variable dq1 "Perception of China"
label define dq1 1 "Friend" 2 "Enemy" 3 "Both" 4 "Neither" 5 "Other answers/DK"
label values dq1 dq1

recode v39 (1=1) (2=2) (3/97=3) (98=.), gen (dq2)
label variable dq2 "Perception of China"
label define dq2 1 "Friend" 2 "Enemy" 3 "Other/DK"
label values dq2 dq2

recode v39 (1=1) (2/97=0) (98=.), gen (dq_friend)
label variable dq_friend "Direct key friend"
label define dq_friend 1 "China is a friend" 0 "China is not a friend"
label values dq_friend dq_friend

recode v39 (2=1) (1 3 4 5/97=0) (98=.), gen (dq_enemy)
label variable dq_enemy "Direct key enemy"
label define dq_enemy 1 "China is an enemy" 0 "China is not an enemy"
label values dq_enemy dq_enemy

gen amb=.
replace amb=0 if ((dq2==1) & (china_image2==3))
replace amb=1 if ((dq2==2) & (china_image2==1))
replace amb=2 if ((dq2==1) & (china_image2<3))
replace amb=2 if ((dq2==2) & (china_image2>1))
replace amb=2 if (dq2==3)
label variable amb "Level of Ambivalence-toward-China"
label define amb 0 "China-positive" 1 "China-negative" 2 "China-ambivalent"
label values amb amb
tab dq2 china_image2, cell


* Summary statistics
sum female age3 education2 ethnicity partyid natl_id friend_base friend_test enemy_base enemy_test cat_friend cat_enemy friend_count enemy_count



// Tables 1 and 2 (Sept 2021 survey results) //
***** Difference-in-means estimator *****
ttest friend_test == friend_base, unpaired unequal welch
ttest enemy_test == enemy_base, unpaired unequal welch

* alternate method 
ttest friend_count, by(cat_friend)
ttest enemy_count, by(cat_enemy)



// Tables 4 and 5 (Sept 2021 survey results) //
***** Imai's ML Estimator *****
* Note: substantial computation time

* Friend list experiment 
est clear
kict ml friend_count female ib3.age3 ib3.education2 ib3.ethnicity ib1.partyid ib3.natl_id ib0.amb, nn(3) cond(cat_friend) est(imai) protect(50) iterate(100) rseed(123)
est store friend
est table friend, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)

* Enemy list experiment
kict ml enemy_count female ib3.age3 ib3.education2 ib3.ethnicity ib1.partyid ib3.natl_id ib0.amb, nn(3) cond(cat_enemy) est(imai) protect(50) iterate(100) rseed(123)
est store enemy
est table enemy, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)
est table friend enemy, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)


// Figures 3 and 4 //

// Figure 3 (right panel) //
reg friend_count cat_friend
eststo qexperiment_friend: margins, dydx(cat_friend) post coefl
eststo qdirect_friend: reg dq_friend 
coefplot qexperiment_friend qdirect_friend, ///
	name(maineffects1, replace) saving(21friend_levsdq.gph, replace) ///
	title("", margin(medlarge)) legend(off) vertical recast(bar) barwidth(0.75) ///
	ciopts(recast(rcap) lwidth(medthick)) citop format(%9.0f) scheme(s2color) ///
	coeflabels(cat_friend = "List experiment" _cons = "Direct question") ///
	ytitle("Estimated proportion", size(medium)) xtitle("") title("2021 September", size(medium)) ///
	yscale(range(.2 (.1) .7)) ylab(.2 (.1) .7, format(%9.1f))
graph display , ysize(1) xsize(1) scale(1) 


// Figure 4 (right panel) //
reg enemy_count cat_enemy
eststo qexperiment_enemy: margins, dydx(cat_enemy) post coefl
eststo qdirect_enemy: reg dq_enemy
coefplot qexperiment_enemy qdirect_enemy, ///
	name(maineffects2, replace) saving(21enemy_levsdq.gph, replace) ///
	title("", margin(medlarge)) legend(off) vertical recast(bar) barwidth(0.75) ///
	ciopts(recast(rcap) lwidth(medthick)) citop format(%9.0f) scheme(s2color) ///
	coeflabels(cat_enemy = "List experiment" _cons = "Direct question") ///
	ytitle("Estimated proportion", size(medium)) xtitle("") title("2021 September", size(medium)) ///
	yscale(range(0 (.1) .7)) ylab(0 (.1) .7, format(%9.1f))
graph display , ysize(1) xsize(1) scale(1) 


***** Appendix *****

// Table A.2.2 //
* Distribution of list experiment responses

tab friend_count cat_friend, col
tab enemy_count cat_enemy, col



*** Treatment randomization (balance tests) ***
// Table A.3.2.3 //
* Friend list experiment
eststo clear
eststo overall: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, columns(statistics) stats(mean sd) missing  
eststo bygroup: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, by(cat_friend) columns(statistics) stats(mean sd) missing  nototal
esttab overall bygroup,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps nonumber varwidth(15) replace wide 
eststo pvalues: quietly estpost ttest female age3 education2 ethnicity partyid natl_id amb, by(cat_friend) 
esttab pvalues, main(b %9.2f) aux(p %9.2f) wide // p-values

// Table A.3.2.4 //
* Enemy list experiment
eststo clear
eststo overall: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, columns(statistics) stats(mean sd) missing  
eststo bygroup: estpost tabstat female age3 education2 ethnicity partyid natl_id amb, by(cat_enemy) columns(statistics) stats(mean sd) missing  nototal
esttab overall bygroup,  main(mean %9.2f) aux(sd %9.2f) unstack  nonote  noobs compress label nogaps nonumber varwidth(15) replace wide 
eststo pvalues: quietly estpost ttest female age3 education2 ethnicity partyid natl_id amb, by(cat_enemy) 
esttab pvalues, main(b %9.2f) aux(p %9.2f) wide // p-values



*** Test for no design effects ***
*use kict deff to estimate the joint probabilities of the answers to the key and nonkey items.

// Table A.3.3.3 //
* Friend list experiment
kict deff friend_count, nnonkey(3) condition(cat_friend) test gms nsim(10000)

// Table A.3.3.4 //
* Enemy list experiment
kict deff enemy_count, nnonkey(3) condition(cat_enemy) test gms nsim(10000)



*** Indirect test for no liars assumption (Floor effects for China as Enemy) ***

// Table A.3.4.1 //
recode enemy_base (0=1) (1/3=0), gen (enemyb_floor)
logit enemyb_floor female ib3.age3 ib3.education2 ib3.ethnicity ib1.partyid ib3.natl_id ib0.amb
est store enemyb_floor
est table enemyb_floor, star(.001 .01 .05) stats(N ll df_m chi2 r2_p aic bic) b(%9.3f) varwidth (30)



// Figures A.4 and A.5 //

* Figure A.4 (right panel)
tabplot amb partyid, percent yreverse height(.7) sep(partyid) bar1(bcolor(blue*0.5)) bar2(bcolor(green*0.5))  ///
  showval ytitle("") subtitle(2021 September, pos(1)) saving(21partyidxamb.gph, replace)
  
* Figure A.5 (right panel)
tabplot amb natl_id, percent yreverse height(.7) sep(natl_id) bar1(bcolor(green*0.5)) bar2(bcolor(red*0.5)) bar3(bcolor(blue*0.5)) ///
  showval ytitle("") subtitle(2021 September, pos(1)) saving(21natl_idxamb.gph, replace)  
